package com.zjy.init;

import com.zjy.business.system.dao.SysRoleDao;
import com.zjy.business.system.domain.entity.SysRole;
import com.zjy.common.constant.StringConstant;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author yxb
 * create_time: 2024-10-24 09:54
 * description: 系统参数初始化器
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class SystemInitHandler {

    final private SysRoleDao sysRoleDao;

    @PostConstruct
    public void init() {
        syncSuperRoleAdmin();
    }

    /**
     * 初始化超级管理员角色
     */
    public void syncSuperRoleAdmin() {
        log.info("初始化超级管理员角色");
        // 检查是否存在超级管理员角色
        Long count = sysRoleDao.lambdaQuery()
                .eq(SysRole::getRoleKey, StringConstant.ROLE_SUPER_ADMIN)
                .count();
        if (count == 0) {
            // 如果不存在,新增
            sysRoleDao.save(createSuperRole());
        }
    }

    private SysRole createSuperRole() {
        SysRole result = new SysRole();
        result.setName("超级管理员");
        result.setRoleKey(StringConstant.ROLE_SUPER_ADMIN);
        result.setStatus(1);
        result.setRemark("超级管理员角色");
        result.setSortValue(1);
        return result;
    }

}
